<template>
  <div class="edit-box">
    <el-dialog width="500px" :title="dialogTitle" v-model="dialogFormVisible" :draggable="true" @close="dialogClose">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules">
        <el-row>
          <el-col :span="12">
            <el-form-item prop="typeId" label="商品类型">
              <el-select style="width:90%" :disabled="dialogForm?.categoryId" v-model="dialogForm.typeId" clearable>
                <el-option v-for="item in typeOptions" :key="item.typeId" :label="item.typeName" :value="item.typeId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="categoryName" label="分类名称">
              <el-input style="width:90%" v-model="dialogForm.categoryName" placeholder="请输入分类名称"
                @keypress.enter="submitHandle" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item prop="showPower" label="权重">
              <el-input style="width:90%" v-model="dialogForm.showPower" type="number" max="999999" maxlength="6"
                placeholder="请输入权重" @keypress.enter="submitHandle" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-form-item prop="password" label="登录密码">
          <el-input v-model="dialogForm.password" type="password" show-password placeholder="请输入管理员登录密码"
            @keypress.enter="submitHandle" />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitHandle">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, toRef, toRefs } from 'vue'
import hexMD5 from "@/utils/md5.min.js"
import api from '../api'
import { warningDeal } from '@/utils/utils'
export default {
  setup(props, context) {
    const dialogFormRef = ref(null)

    const state = reactive({
      dialogTitle: '',
      dialogFormVisible: false,
      dialogForm: {},
      dialogFormRules: {
        typeId: [
          { required: true, message: '请选择商品类型', trigger: 'blur' },
        ],
        categoryName: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入管理员登录密码', trigger: 'blur' },
        ],
      },
      typeOptions: [],
    })

    function getTypeOptions() {
      api.goodsTypeListSimple().then(res => {
        if (res.code == '200') {
          state.typeOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function dialogShow(data) {
      state.dialogTitle = data ? '编辑商品分类' : '新增商品分类'
      state.dialogForm = { ...data } || {}
      state.dialogFormVisible = true
      getTypeOptions()
    }

    function submitHandle() {
      dialogFormRef.value.validate((valid, fields) => {
        if (valid) {
          if (state.dialogForm.categoryId) {
            editType()
          } else {
            addType()
          }
        }
      })
    }

    function addType() {
      const obj = {
        typeId: state.dialogForm?.typeId,
        categoryName: state.dialogForm?.categoryName,
        showPower: state.dialogForm?.showPower,
        // password: hexMD5(state.dialogForm.password),
      }
      api.goodsCategoryAdd(obj).then(res => {
        if (res.code == '200') {
          context.emit('dialogClose')
          state.dialogFormVisible = false
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function editType() {
      const obj = {
        categoryId: state.dialogForm?.categoryId,
        typeId: state.dialogForm?.typeId,
        categoryName: state.dialogForm?.categoryName,
        showPower: state.dialogForm?.showPower,
        // password: hexMD5(state.dialogForm.password),
      }
      api.goodsCategoryModify(obj).then(res => {
        if (res.code == '200') {
          context.emit('dialogClose')
          state.dialogFormVisible = false
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function dialogClose() {
      dialogFormRef.value.resetFields()
    }

    return {
      ...toRefs(state),
      dialogShow,
      dialogFormRef,
      dialogClose,
      submitHandle,
    }
  }
}
</script>

<style>
</style>